#include <iostream>
using namespace std;

typedef struct rule
{
    int line;  //纳税线
    int ratio; //税率
} Rule;

Rule *myRule; //结构体数组
int ruleCnt;  //条数
int main()
{
    void inputRules(Rule * r);     //录入规则
    double computeTax(int income); //计算所得税
    int income;
    inputRules(myRule);
    cout << "Entry to complete!" << endl;
    for (int i = 0; i < ruleCnt; i++)
    {
        cout << i + 1 << " : " << myRule[i].line << "\t" << myRule[i].ratio << endl;
    }

    while (1)
    {
        cout << "Please enter your income : ";
        cin >> income;
        int taxes = computeTax(income);
        if (income == -1)
            break;
        cout << "Your income is " << income << " ,  should pay taxes is " << taxes << " yuan " << endl;
    }
    delete myRule;
    myRule == NULL;
    return 0;
}
//录入规则
void inputRules(Rule *r)
{
    cout << "Please enter ruleCnt : ";
    cin >> ruleCnt;
    r = new Rule[ruleCnt];
    for (int i = 0; i < ruleCnt; i++)
    {
        cout << "Please enter the " << i + 1 << " rule : ";
        cin >> r[i].line;
        cin >> r[i].ratio;
    }
}
//计算所得税
double computeTax(int income)
{
    double sum = 0; //税总和
    int i = 0;
    double gap; //两个line的差
    while (income > myRule[i].line)
    {
        if (income > myRule[i + 1].line)
            gap = myRule[i + 1].line - myRule[i].line;
        else
            gap = income - myRule[i].line;
        sum += gap * myRule[i].ratio;
    }
    if (sum != 0)
    {
        sum /= 100.0;
    }
    return sum;
}

